//
//  UserHandler.swift
//  COpenSSL
//
//  Created by 陈志勇 on 2018/11/12.
//

import PerfectHTTP
import PerfectLogger
import PerfectLib

class UserHandler {
    static func login(request: HTTPRequest, _ response: HTTPResponse) {
        guard let userName = request.param(name: "userName") else {
            return
        }
        
        guard let password = request.param(name: "password") else {
            return
        }
        
        guard let json = UserOperator().queryUserInfo(userName: userName, password: password) else {
            LogFile.error("json为nil")
            response.completed()
            return
        }
        
        LogFile.info(json)
        response.setBody(string: json)
        response.completed()
    }
    
    static func create(request: HTTPRequest, _ response: HTTPResponse) {
        guard let username = request.param(name: "userName") else {
            LogFile.error("userName为nil")
            response.completed()
            return
        }
        
        guard let password = request.param(name: "password") else {
            LogFile.error("password为nil")
            response.completed()
            return
        }
        guard let json = UserOperator().insertUserInfo(userName: username, password: password) else {
            LogFile.error("json为nil")
            response.completed()
            return
        }
        LogFile.info(json)
        response.setBody(string: json)
        response.completed()
    }

}
